#! /usr/bin/env python
# coding=utf-8

# 使用tblastn 对每个指定物种运行


import sys
import argparse

dis = '''
对制定物种运行 tblastn 
tblastn_fetch_gene01_run_tblast_for_target_species.py -d db -q query.fa -o tblastn_res -t 8 -i target_lista
'''


parser = argparse.ArgumentParser(description=dis)



parser.add_argument('-d',
                help='必须给定，db路径')

parser.add_argument('-i',
                help='必须给定，目标物种列表文件')

parser.add_argument('-o',
                help='必须给定，输出路径')

parser.add_argument('-q',
                help='必须给定，query路径')

parser.add_argument('-t',
                help='并行核数 默认为8')


# 获取参数
args = parser.parse_args()



if not args.i or not args.q or not args.d or not args.o:
    parser.print_help()
    sys.exit()


inpath = args.d

query_file = args.q

target_file = args.i

result_file = args.o

if not args.t:
	thread = 8

thread = int(args.t)


target_lista= []
with open(target_file) as fila:
	for i in fila:
		k = i.strip().split('\t')[0].strip()
		if k!='':
			target_lista.append(k)

import os
import shutil


# try:
# 	shutil.rmtree(result_file)
# except:
# 	pass
try:
	os.mkdir(result_file)
except:
	pass


for i in target_lista:
	if i.strip()!='':
		cmd = "tblastn -db %s/%s -query %s  -out %s/%s -num_threads %s -outfmt 6 -max_target_seqs 1" %(inpath, i, query_file, result_file, i, thread)
		os.system(cmd)